package utils

import (
	"io/ioutil"
	"os"
	"path/filepath"
	"strconv"
	"strings"
)

type FilesList struct {
	FileList []FileListStruct
}

type FileListStruct struct {
	Name string
	Type string
	Size string
	Time string
	Url  string
}

func (fl *FilesList) GetFileList(url []string) {

	fileType := map[string][]string{ //根据拓展名定义文件类型
		"picture": {"jpg", "jpeg", "gif", "png", "bmp"},
		"video":   {"mp4", "3gp", "avi", "flv", "wmv", "mkv"},
		"btfile":  {"torrent"},
		"audio":   {"mp3"},
		"other":   {"mpg", "rmvb", "rm", "vob", "mov"},
	}
	sdirs := url
	// filesLists := []FileListStruct{} //存放retrun列表
	for len(sdirs) != 0 { //判断文件夹列表内是否有数据
		sdir := sdirs[0]                                    //获取第一个数据
		sdirs = sdirs[1:]                                   //将获取的数据清除
		if sdir[len(sdir)-1:] != string(os.PathSeparator) { //判断获取的文件夹后面是否有/
			sdir = sdir + string(os.PathSeparator) //如果没有，添加/
		}
		files, _ := ioutil.ReadDir(sdir) //获取目录内的文件和文件夹
		for _, f := range files {        //迭代获取到的内容
			if f.IsDir() { //判断是否为文件夹
				// var dirlist DirsList //定义一个文件类型
				// dirlist.Name = f.Name()
				dirtmp, _ := filepath.Abs(sdir + f.Name()) //获取绝对路径
				sdirs = append(sdirs, dirtmp)
			} else {
				var filelist FileListStruct
				filelist.Name = f.Name()
				filetzm := filepath.Ext(filelist.Name)
				if filetzm != "" {
					filetzm = strings.ToLower(filetzm[1:])
					// fmt.Println("filetzm", filetzm)
					var t string
					for ts, key := range fileType {
						for _, t = range key {
							// fmt.Println("t", t, "tzm", filetzm)
							if t == filetzm {
								filelist.Type = ts
								break
							}

						}
						if t == "" {
							break
						}
					}
					dirtmp, _ := filepath.Abs(sdir)
					filelist.Url = dirtmp + string(os.PathSeparator)
					filelist.Size = strconv.FormatInt(f.Size(), 10)
					fl.FileList = append(fl.FileList, filelist)
				}

			}
		}
	}
	// fl.FileList=
	// return fl

}
